home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / INDENT3.ARJ / PR_COMME.C < prev    next >
C/C++ Source or Header  |  1992-08-04  |  12KB  |  380 lines

  1. /* Copyright (c) 1980 Regents of the University of California. All rights
  2.  * reserved.  The Berkeley software License Agreement specifies the terms and
  3.  * conditions for redistribution. */
  4.  
  5. #ifndef lint
  6. static char sccsid[] = "@(#)pr_comment.c    5.2 (Berkeley) 8/28/85";
  7. #endif not lint
  8.  
  9. /*-
  10.  *
  11.  *              Copyright (C) 1976
  12.  *                by the
  13.  *              Board of Trustees
  14.  *                of the
  15.  *            University of Illinois
  16.  *
  17.  *             All rights reserved
  18.  *
  19.  *
  20.  * NAME:
  21.  *    pr_comment
  22.  *
  23.  * FUNCTION:
  24.  *    This routine takes care of scanning and printing comments.
  25.  *
  26.  * ALGORITHM:
  27.  *    1) Decide where the comment should be aligned, and if lines should
  28.  *       be broken.
  29.  *    2) If lines should not be broken and filled, just copy up to end of
  30.  *       comment.
  31.  *    3) If lines should be filled, then scan thru input_buffer copying
  32.  *       characters to com_buf.  Remember where the last blank, tab, or
  33.  *       newline was.  When line is filled, print up to last blank and 
  34.  *       continue copying.
  35.  *
  36.  * HISTORY:
  37.  *    November 1976    D A Willcox of CAC    Initial coding
  38.  *    12/6/76        D A Willcox of CAC    Modification to handle 
  39.  *                        UNIX-style comments
  40.  *
  41.  */
  42.  
  43. /* this routine processes comments.  It makes an attempt to keep comments
  44.  * from going over the max line length.  If a line is too long, it moves
  45.  * everything from the last blank to the next comment line.  Blanks and tabs
  46.  * from the beginning of the input line are removed */
  47.  
  48. #include "indent_globs.h"
  49.  
  50. void pr_comment()
  51. {
  52.     int now_col;        /* column we are in now */
  53.     int adj_max_col;        /* Adjusted max_col for when we decide to
  54.                  * spill comments over the right margin */
  55.     char *last_bl;        /* points to the last blank in the output
  56.                  * buffer */
  57.     char achar;
  58.     char *t_ptr;        /* used for moving string */
  59.     int unix_comment;        /* tri-state variable used to decide if it is
  60.                  * a unix-style comment. 0 means only blanks
  61.                  * since /*, 1 means regular style comment, 2
  62.                  * means unix style comment */
  63.     int break_delim = comment_delimiter_on_blankline;
  64.     int l_just_saw_decl = ps.just_saw_decl;
  65.     /* int         ps.last_nl = 0;      /* true iff the last significant
  66.      * thing weve seen is a newline */
  67.     int one_liner = 1;        /* true iff this comment is a one-liner */
  68.     adj_max_col = max_col;
  69.     ps.just_saw_decl = 0;
  70.     last_bl = 0;        /* no blanks found so far */
  71.     ps.box_com = false;        /* at first, assume that we are not in a
  72.                  * boxed comment or some other comment that
  73.                  * should not be touched */
  74.     ++ps.out_coms;        /* keep track of number of comments */
  75.     unix_comment = 0;        /* set flag to let us figure out if there is
  76.                  * a unix-style comment ** DISABLED: use 0 to
  77.                  * reenable this hack! */
  78.  
  79.     /* Figure where to align and how to treat the comment */
  80.  
  81.     if (ps.col_1 && !format_col1_comments) {    /* if comment starts in
  82.                          * column 1 it should not be
  83.                          * touched */
  84.     ps.box_com = true;
  85.     ps.com_col = 1;
  86.     } else {
  87.     if (*buf_ptr == '-' || *buf_ptr == '*' || !format_comments) {
  88.         ps.box_com = true;    /* a comment with a '-' or '*' immediately
  89.                  * after the '/*' is assumed to be a boxed
  90.                  * comment */
  91.         break_delim = 0;
  92.     }
  93.     if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
  94.         /* klg: check only if this line is blank */
  95.         /* If this (*and previous lines are*) blank, dont put comment way
  96.          * out at left */
  97.         ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
  98.         adj_max_col = block_comment_max_col;
  99.         if (ps.com_col <= 1)
  100.         ps.com_col = 1 + !format_col1_comments;
  101.     } else {
  102.         register target_col;
  103.         break_delim = 0;
  104.         if (s_code != e_code)
  105.         target_col = count_spaces(compute_code_target(), s_code);
  106.         else {
  107.         target_col = 1;
  108.         if (s_lab != e_lab)
  109.             target_col = count_spaces(compute_label_target(), s_lab);
  110.         }
  111.         ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind;
  112.         if (ps.com_col < target_col)
  113. #if 0
  114.         ps.com_col = ((target_col + tabsize - 1) & tabmask) + 1;
  115. #else
  116.         ps.com_col = target_col + tabsize - (target_col % tabsize) + 1;
  117. #endif
  118.         if (ps.com_col + 24 > adj_max_col)
  119.         adj_max_col = ps.com_col + 24;
  120.     }
  121.     }
  122.     if (ps.box_com) {
  123.     buf_ptr[-2] = 0;
  124.     ps.n_comment_delta = 1 - count_spaces(1, in_buffer);
  125.     ps.comment_delta = 0;
  126.     buf_ptr[-2] = '/';
  127.     } else {
  128.     ps.n_comment_delta = 0;
  129.     ps.comment_delta = 0;
  130.     while (*buf_ptr == ' ' || *buf_ptr == '\t')
  131.         buf_ptr++;
  132.     }
  133.     ps.comment_delta = 0;
  134.     *e_com++ = '/';        /* put '/*' into buffer */
  135.     *e_com++ = '*';
  136.     if (*buf_ptr != ' ' && !ps.box_com)
  137.     *e_com++ = ' ';
  138.  
  139.     *e_com = '\0';
  140.     now_col = count_spaces(ps.com_col, s_com);    /* figure what column we
  141.                          * would be in if we printed
  142.                          * the comment now */
  143.  
  144.     /* Start to copy the comment */
  145.  
  146.     while (1) {            /* this loop will go until the comment is
  147.                  * copied */
  148.     if (*buf_ptr > 040 && *buf_ptr != '*')
  149.         ps.last_nl = 0;
  150.     switch (*buf_ptr) {    /* this checks for various spcl cases */
  151.         case '\f':        /* check for a form feed */
  152.         if (!ps.box_com) {    /* in a text comment, break the line
  153.                      * here */
  154.             ps.use_ff = true;
  155.             /* fix so dump_line uses a form feed */
  156.             dump_line();
  157.             last_bl = 0;
  158.             *e_com++ = ' ';
  159.             *e_com++ = '*';
  160.             *e_com++ = ' ';
  161.             while (*++buf_ptr == ' ' || *buf_ptr == '\t');
  162.         } else {
  163.             if (++buf_ptr >= buf_end)
  164.             fill_buffer();
  165.             *e_com++ = '\f';
  166.         }
  167.         break;
  168.  
  169.         case '\n':
  170.         if (had_eof) {    /* check for unexpected eof */
  171.             printf("Unterminated comment\n");
  172.             *e_com = '\0';
  173.             dump_line();
  174.             return;
  175.         }
  176.         one_liner = 0;
  177.         if (ps.box_com || ps.last_nl) {        /* if this is a boxed
  178.                              * comment, we dont
  179.                              * ignore the newline */
  180.             if (s_com == e_com) {
  181.             *e_com++ = ' ';
  182.             *e_com++ = ' ';
  183.             }
  184.             *e_com = '\0';
  185.             if (!ps.box_com && e_com - s_com > 3) {
  186.             if (break_delim == 1 && s_com[0] == '/'
  187.                 && s_com[1] == '*' && s_com[2] == ' ') {
  188.                 char *t = e_com;
  189.                 break_delim = 2;
  190.                 e_com = s_com + 2;
  191.                 *e_com = 0;
  192.                 if (blanklines_before_blockcomments)
  193.                 prefix_blankline_requested = 1;
  194.                 dump_line();
  195.                 e_com = t;
  196.                 s_com[0] = s_com[1] = s_com[2] = ' ';
  197.             }
  198.             dump_line();
  199.             *e_com++ = ' ';
  200.             *e_com++ = ' ';
  201.             }
  202.             dump_line();
  203.             now_col = ps.com_col;
  204.         } else {
  205.             ps.last_nl = 1;
  206.             if (unix_comment != 1) {    /* we not are in unix_style
  207.                          * comment */
  208.             if (unix_comment == 0 && s_code == e_code) {
  209.                 /* if it is a UNIX-style comment, ignore the
  210.                  * requirement that previous line be blank for
  211.                  * unindention */
  212.                 ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
  213.                 if (ps.com_col <= 1)
  214.                 ps.com_col = 2;
  215.             }
  216.             unix_comment = 2;    /* permanently remember that
  217.                          * we are in this type of
  218.                          * comment */
  219.             dump_line();
  220.             ++line_no;
  221.             now_col = ps.com_col;
  222.             *e_com++ = ' ';
  223.             /* fix so that the star at the start of the line will
  224.              * line up */
  225.             do    /* flush leading white space */
  226.                 if (++buf_ptr >= buf_end)
  227.                 fill_buffer();
  228.             while (*buf_ptr == ' ' || *buf_ptr == '\t');
  229.             break;
  230.             }
  231.             if (*(e_com - 1) == ' ' || *(e_com - 1) == '\t')
  232.             last_bl = e_com - 1;
  233.             /* if there was a space at the end of the last line,
  234.              * remember where it was */
  235.             else {    /* otherwise, insert one */
  236.             last_bl = e_com;
  237.             *e_com++ = ' ';
  238.             ++now_col;
  239.             }
  240.         }
  241.         ++line_no;    /* keep track of input line number */
  242.         if (!ps.box_com) {
  243.             int nstar = 1;
  244.             do {    /* flush any blanks and/or tabs at start of
  245.                  * next line */
  246.             if (++buf_ptr >= buf_end)
  247.                 fill_buffer();
  248.             if (*buf_ptr == '*' && --nstar >= 0) {
  249.                 if (++buf_ptr >= buf_end)
  250.                 fill_buffer();
  251.                 if (*buf_ptr == '/')
  252.                 goto end_of_comment;
  253.             }
  254.             } while (*buf_ptr == ' ' || *buf_ptr == '\t');
  255.         } else if (++buf_ptr >= buf_end)
  256.             fill_buffer();
  257.         break;        /* end of case for newline */
  258.  
  259.         case '*':        /* must check for possibility of being at end
  260.                  * of comment */
  261.         if (++buf_ptr >= buf_end)    /* get to next char after '*' */
  262.             fill_buffer();
  263.  
  264.         if (unix_comment == 0)    /* set flag to show we are not in
  265.                      * unix-style comment */
  266.             unix_comment = 1;
  267.  
  268.         if (*buf_ptr == '/') {    /* it is the end!!! */
  269.         end_of_comment:
  270.             if (++buf_ptr >= buf_end)
  271.             fill_buffer();
  272.  
  273.             if (*(e_com - 1) != ' ' && !ps.box_com) {    /* insure blank before
  274.                                  * end */
  275.             *e_com++ = ' ';
  276.             ++now_col;
  277.             }
  278.             if (break_delim == 1 && !one_liner && s_com[0] == '/'
  279.             && s_com[1] == '*' && s_com[2] == ' ') {
  280.             char *t = e_com;
  281.             break_delim = 2;
  282.             e_com = s_com + 2;
  283.             *e_com = 0;
  284.             if (blanklines_before_blockcomments)
  285.                 prefix_blankline_requested = 1;
  286.             dump_line();
  287.             e_com = t;
  288.             s_com[0] = s_com[1] = s_com[2] = ' ';
  289.             }
  290.             if (break_delim == 2 && e_com > s_com + 3
  291.              /* now_col > adj_max_col - 2 && !ps.box_com */ ) {
  292.             *e_com = '\0';
  293.             dump_line();
  294.             now_col = ps.com_col;
  295.             }
  296.             *e_com++ = '*';
  297.             *e_com++ = '/';
  298.             *e_com = '\0';
  299.             ps.just_saw_decl = l_just_saw_decl;
  300.             return;
  301.         } else {    /* handle isolated '*' */
  302.             *e_com++ = '*';
  303.             ++now_col;
  304.         }
  305.         break;
  306.         default:        /* we have a random char */
  307.         if (unix_comment == 0 && *buf_ptr != ' ' && *buf_ptr != '\t')
  308.             unix_comment = 1;    /* we are not in unix-style comment */
  309.  
  310.         *e_com = *buf_ptr++;
  311.         if (buf_ptr >= buf_end)
  312.             fill_buffer();
  313.  
  314.         if (*e_com == '\t') {    /* keep track of column */
  315. #if 0
  316.             now_col = ((now_col - 1) & tabmask) + tabsize + 1;
  317. #else
  318.             now_col += tabsize - (now_col % tabsize) + 1;
  319. #endif
  320.         } else if (*e_com == '\b')    /* this is a backspace */
  321.             --now_col;
  322.         else
  323.             ++now_col;
  324.  
  325.         if (*e_com == ' ' || *e_com == '\t')
  326.             last_bl = e_com;
  327.         /* remember we saw a blank */
  328.  
  329.         ++e_com;
  330.         if (now_col > adj_max_col && !ps.box_com && unix_comment == 1 && e_com[-1] > ' ') {
  331.             /* the comment is too long, it must be broken up */
  332.             if (break_delim == 1 && s_com[0] == '/'
  333.             && s_com[1] == '*' && s_com[2] == ' ') {
  334.             char *t = e_com;
  335.             break_delim = 2;
  336.             e_com = s_com + 2;
  337.             *e_com = 0;
  338.             if (blanklines_before_blockcomments)
  339.                 prefix_blankline_requested = 1;
  340.             dump_line();
  341.             e_com = t;
  342.             s_com[0] = s_com[1] = s_com[2] = ' ';
  343.             }
  344.             if (last_bl == 0) {        /* we have seen no blanks */
  345.             last_bl = e_com;    /* fake it */
  346.             *e_com++ = ' ';
  347.             }
  348.             *e_com = '\0';    /* print what we have */
  349.             *last_bl = '\0';
  350.             while (last_bl > s_com && last_bl[-1] < 040)
  351.             *--last_bl = 0;
  352.             e_com = last_bl;
  353.             dump_line();
  354.  
  355.             *e_com++ = ' ';    /* add blanks for continuation */
  356.             *e_com++ = ' ';
  357.             *e_com++ = ' ';
  358.  
  359.             t_ptr = last_bl + 1;
  360.             last_bl = 0;
  361.             if (t_ptr >= e_com) {
  362.             while (*t_ptr == ' ' || *t_ptr == '\t')
  363.                 t_ptr++;
  364.             while (*t_ptr != '\0') {    /* move unprinted part
  365.                              * of comment down in
  366.                              * buffer */
  367.                 if (*t_ptr == ' ' || *t_ptr == '\t')
  368.                 last_bl = e_com;
  369.                 *e_com++ = *t_ptr++;
  370.             }
  371.             }
  372.             *e_com = '\0';
  373.             now_col = count_spaces(ps.com_col, s_com);    /* recompute current
  374.                                  * position */
  375.         }
  376.         break;
  377.     }
  378.     }
  379. }
  380.